package com.weimob.mcloud.wechatwork.customer.facade.impl.customer;

import com.weimob.mcloud.wechatwork.core.adapter.enums.cdp.CdpMarkMessageEventEnum;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.cdp.response.customer.CDPUserLogBO;
import com.weimob.mcloud.wechatwork.customer.domain.ActivityMarkInfo;
import com.weimob.mcloud.wechatwork.customer.domain.ProductMarkDomain;
import com.weimob.mcloud.wechatwork.customer.domain.UserBehaviorRecord;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/9/14 8:33 下午
 */
public class CdpMarkRecordCommonService {

    /**
     * 打点记录
     * @param userBehaviorDTO
     * @param cdpMarkMessageEventEnum
     * @return
     */
    public static UserBehaviorRecord handleSpecialCdpMarkRecord(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO, UserBehaviorRecord userBehaviorRecord,
                                                                  CdpMarkMessageEventEnum cdpMarkMessageEventEnum) {

        if (Objects.isNull(userBehaviorDTO) || Objects.isNull(cdpMarkMessageEventEnum)) {
            return userBehaviorRecord;
        }
        String dataJson = userBehaviorDTO.getDataJson();

        switch (cdpMarkMessageEventEnum) {

            case SEARCH:

                //搜索
                return buildSearch(userBehaviorDTO, userBehaviorRecord);

            case JOIN_GROUP:

                //加入群聊
                return buildJoinGroup(userBehaviorDTO, userBehaviorRecord);
            case QUIT_GROUP:

                //退出群聊
                return buildQuitGroup(userBehaviorDTO, userBehaviorRecord);
            case JOIN:

                //参与裂变活动
                return buildJoin(userBehaviorDTO, userBehaviorRecord);
            case MISSION_S:

                //完成裂变任务
                return buildMissions(userBehaviorDTO, userBehaviorRecord);
            case BROWSE_CONTENT_ACTIVITY:

                //浏览内容主题页
                return browsEcontentActity(userBehaviorDTO, userBehaviorRecord);
            case CART:

                //加购
                return cart(userBehaviorDTO, userBehaviorRecord);
        }

       return userBehaviorRecord;

    }



    /**
     * 搜索
     * @param userBehaviorDTO
     * @return
     */
    private static UserBehaviorRecord buildSearch(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                   UserBehaviorRecord userBehaviorRecord) {

        //搜索词
        userBehaviorRecord.setValue(userBehaviorDTO.getKeyword());

        return userBehaviorRecord;
    }


    /**
     * 加入群聊
     * @param userBehaviorDTO
     * @return
     */
    private static UserBehaviorRecord buildJoinGroup(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                      UserBehaviorRecord userBehaviorRecord) {

        userBehaviorRecord.setValue(userBehaviorDTO.getGroupName());
        return userBehaviorRecord;
    }

    /**
     * 退出群聊
     * @param userBehaviorDTO
     * @return
     */
    private static UserBehaviorRecord buildQuitGroup(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                      UserBehaviorRecord userBehaviorRecord) {
        userBehaviorRecord.setValue(userBehaviorDTO.getGroupName());
        return userBehaviorRecord;
    }

    /**
     * 参与裂变活动
     * @param userBehaviorDTO
     * @return
     */
    private static UserBehaviorRecord buildJoin(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                 UserBehaviorRecord userBehaviorRecord) {

        userBehaviorRecord.setValue(userBehaviorDTO.getActivityName());
        return userBehaviorRecord;
    }

    /**
     * 完成裂变任务
     * @param userBehaviorDTO
     * @return
     */
    private static UserBehaviorRecord buildMissions(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                     UserBehaviorRecord userBehaviorRecord) {

        userBehaviorRecord.setValue(userBehaviorDTO.getActivityName());
        return userBehaviorRecord;
    }

    private static UserBehaviorRecord browsEcontentActity(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                            UserBehaviorRecord userBehaviorRecord) {
        ActivityMarkInfo activity = userBehaviorRecord.getActivity();
        if (Objects.nonNull(activity)) {
            activity.setActivityName(userBehaviorDTO.getPageTitle());
        }
        return userBehaviorRecord;
    }

    /**
     * 加购
     * @param userBehaviorDTO
     * @param userBehaviorRecord
     * @return
     */
    private static UserBehaviorRecord cart(CDPUserLogBO.UserBehaviorDTO userBehaviorDTO,
                                                              UserBehaviorRecord userBehaviorRecord) {
        ProductMarkDomain product = userBehaviorRecord.getProduct();
        if (Objects.nonNull(product)) {
            BigDecimal skuPrice = userBehaviorDTO.getSkuPrice();
            if (Objects.nonNull(skuPrice)) {
                product.setPrice(skuPrice.doubleValue());
            }

        }
        userBehaviorRecord.setStoreName(userBehaviorDTO.getStoreName());
        return userBehaviorRecord;
    }




}
